﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class AllLevelViewModel:WorkspaceViewModel
    {
        private ObservableCollection<LevelViewModel> _lstLevelViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _findCommand;
        private DelegateCommand _addCommand;
        private DelegateCommand _deleteCommand;
        private IQueryable<LevelViewModel> _query;
        private string _levelName;
        public AllLevelViewModel(icanDataContext dataContext)
        {
            _lstLevelViewModel = new ObservableCollection<LevelViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_AllLevel;
        }
        public AllLevelViewModel(icanDataContext dataContext, bool isGetAll)
        {
            _lstLevelViewModel = new ObservableCollection<LevelViewModel>();
            _dataContext = dataContext;
            if (isGetAll)
            {
                _query = from level in _dataContext.Levels orderby level.ID descending select new LevelViewModel(_dataContext, level);
                this.NumRow = _query.Count();
                this.CurPage = 1;
                this.UpdateList();
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_AllLevel;
        }
        public string Name
        {
            get { return _levelName; }
            set { _levelName = value; }
        }
        
        public ObservableCollection<LevelViewModel> AllLevels
        {
            get { return _lstLevelViewModel; }
            set {
                _lstLevelViewModel = value;
                base.OnPropertyChanged("AllLevels");
            }
        }
        public ICommand FindCommand
        {
            get
            {
                if (_findCommand == null)
                {
                    _findCommand = new DelegateCommand(Find);
                }
                return _findCommand;
            }
        }
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(AddLevel);
                }
                return _addCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand(DeleteLevel, CanDeleteLevel);
                }
                return _deleteCommand;
            }
        }
        
        public void Find()
        {
            IQueryable<Level> myQuery = _dataContext.Levels;
            if (!string.IsNullOrEmpty(_levelName))
                myQuery = myQuery.Where<Level>(level => level.Name.Contains(_levelName));
            //Add more Where Conditions here
            
            //Order
            myQuery = myQuery.OrderByDescending(l => l.ID);
            //Select
            _query = myQuery.Select(level => new LevelViewModel(_dataContext, level));
            this.NumRow = _query.Count();
            //Reset to page 0
            this.CurPage = 1;
            this.UpdateList();
        }
        
        public void AddLevel()
        {
            LevelViewModel ws = new LevelViewModel(_dataContext);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, ws);
        }
        
        private void DeleteLevel()
        {
            foreach (LevelViewModel lvm in AllLevels)
            {
                if(lvm.IsSelected==true)
                    lvm.Delete();
            }
            this.NumRow = _query.Count();
            this.UpdateList();

        }
        private bool CanDeleteLevel()
        {
            if (ViewModelBase.CurrentRole != UserRole.Administrator)
                return false;

            LevelViewModel lVM = AllLevels.FirstOrDefault(l => l.IsSelected == true);
            if (lVM == null)
                return false;
            return lVM.CanDelete;
        }
        [MediatorLib.MediatorMessageSink(Messages.ListLevelUpdated, ParameterType = typeof(Level))]
        public void UpdateListLevel(Level level)
        {
            this.NumRow = _query.Count();
            this.UpdateList();
        }

        public override void UpdateList()
        {
            List<LevelViewModel> lstLevels = _query.Skip((this.CurPage - 1) * this.RowsPerPage).Take(this.RowsPerPage).ToList();
            AllLevels = new ObservableCollection<LevelViewModel>(lstLevels);
            base.UpdateList();
        }
    }
}
